package UILayer;

import java.util.Scanner;
import CtrLayer.*;
import ModLayer.*;
import java.util.ArrayList;

/**
 * Write a description of class LeaseUI here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class LeaseUI
{
    private LeaseCtr leaseCtr;
    private TimeAndCheck time;
    private PersonCtr personCtr;
    
    public LeaseUI()
    {
        leaseCtr = leaseCtr.getInstance();
        time = time.getInstance();
        personCtr = personCtr.getInstance();
    }

    public void start()
    { 
        boolean finished = false; 
        while(!finished)
        {
            printMenu();
            switch(reader(4)) 
            { 
                case "0": finished = true; break; 
                case "1": removeLease(); break; 
                case "2": createLease(); break;
                case "3": returnMenu(); break; 
                case "4": searchLease(); break;
            } 
        } 
    }

    private void printMenu()
    { 
        System.out.println("\nWhat would you like to do?"); 
        System.out.println("(0) Back"); 
        System.out.println("(1) Remove lease"); 
        System.out.println("(2) Add lease"); 
        System.out.println("(3) Return Lease");
        System.out.println("(4) Search Lease");
        System.out.println("Make your choice: "); 
    }

    public void returnMenu()
    {
        System.out.println("Type in the id of the lease you wish to return.");
        String read = reader2();
        if(read != "0")
        {
            if(leaseCtr.findLease(read)!=null)
            {
                checkout(read);
                System.out.println("Checkout successful");
            }
            else
                System.out.println("No such Sale was found.");
        }

    }

    public void checkout(String id)
    {
        System.out.println("Type in the discount percentage you have negociated.");
        double discount = Double.parseDouble(reader2());
        leaseCtr.checkout(id, discount);
        Lease l = leaseCtr.findLease(id);
        if(leaseCtr.isCustomerRegistered(l))
        {
            System.out.println("\nName of company: "+personCtr.getInfoCompany("2"));
            System.out.println("Address of company: "+personCtr.getInfoCompany("0"));
            System.out.println("Phone number of company: "+personCtr.getInfoCompany("3"));
            System.out.println("Email of company: "+personCtr.getInfoCompany("1"));
            System.out.println("Fax number of company: "+personCtr.getInfoCompany("5"));
            System.out.println("Bank account number of company: "+personCtr.getInfoCompany("4"));
        }
        System.out.println("Customer id is: " + l.getIDCustomer());
        System.out.println("Employee id is: " + l.getIDEmployee());
        System.out.println("Product id is: " + l.getIDProduct());
        leaseCtr.removeLease(id);
    }
    
    public void printLease(Lease l)
    {
        if(leaseCtr.isCustomerRegistered(l))
        {
            System.out.println("\nName of company: "+personCtr.getInfoCompany("2"));
            System.out.println("Address of company: "+personCtr.getInfoCompany("0"));
            System.out.println("Phone number of company: "+personCtr.getInfoCompany("3"));
            System.out.println("Email of company: "+personCtr.getInfoCompany("1"));
            System.out.println("Fax number of company: "+personCtr.getInfoCompany("5"));
            System.out.println("Bank account number of company: "+personCtr.getInfoCompany("4"));
        }
        System.out.println("Customer id is: " + l.getIDCustomer());
        System.out.println("Employee id is: " + l.getIDEmployee());
        System.out.println("Product id is: " + l.getIDProduct());
        System.out.println("Start Date is: " + l.getStartDate());
        System.out.println("Duration: " + l.getDuration());
    }

    public void createLease()
    {
        boolean finished = false; 
        System.out.println("\f*** Add sale ***");
        String id = time.getNoForCustomer();
        System.out.println("\nType in id code of the product.");
        String p = reader2();
        System.out.println("\nType in id code of employee.");
        String c = reader2();
        System.out.println("\nType in id of customer or bank account info.");
        String e = reader2();
        String a = time.getActualDate();
        System.out.println("\nType in the duration of the lease.");
        int d = Integer.parseInt(reader2());
        leaseCtr.createLease(p, c, e, a, d);
    }

    public void removeLease()
    {
        System.out.println("\f***Remove lease***");
        System.out.println("Enter lease id");
        String read = reader2();
        if(read != "0")
        {
            if(leaseCtr.findLease(read)!=null)
            {
                leaseCtr.removeLease(read);
                System.out.println("Lease successfully deleted.");
            }
            else
                System.out.println("No such Lease was found.");
        }
    }

    public void searchLease()
    {
        for(Lease x : leaseCtr.searchLease())
        {
            printLease(x);
        }
    }

    private String reader2()
    {
        Scanner command = new Scanner(System.in); 
        System.out.print(">> "); 
        String c = command.nextLine(); 
        return c;
    }

    private String reader(int limitIndex)
    { 
        final String[] validNumbers = {"0","1","2","3","4","5","6","7","8","9"}; 
        Scanner command = new Scanner(System.in); 
        String c = null; 
        boolean finished = false;
        while(!finished)
        { 
            System.out.print("\n> "); 
            c = command.nextLine();
            for(int i=0; i<=limitIndex; i++)
            {
                if(validNumbers[i].equals(c)) 
                    finished = true;
            }
            if(finished == false) 
            {
                System.out.print("\nTry once again.");
                c = null;
            }
        }
        return c; 
    }
}